package com.yanq.liancar.activity;
/*
 *  @创建者     Administrator
 *  @创建时间   2018/11/29  15:43
 *  @描述       ${TODO}
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxSPTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.ProtocolAdapter;
import com.yanq.liancar.bean.BShopGoodsListBean;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.ProtocolListBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.view.recyclerview.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ProtocolListActivity extends BaseToolbarActivity {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.tv_qianyue)
    TextView tvQianyue;
    @BindView(R.id.tv_content)
    TextView tv_content;
    private int status;
    private List<ProtocolListBean.DataBean> mList = new ArrayList<>();
    private ProtocolAdapter mProtocolAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_protocollist);
        ButterKnife.bind(this);
        mTvTitle.setText("协议列表");
        ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.white).init();

        status=getIntent().getIntExtra("type",0);

        if (status==1){
            tvQianyue.setVisibility(View.VISIBLE);
            tv_content.setVisibility(View.VISIBLE);
        }else {
            tvQianyue.setVisibility(View.GONE);
            tv_content.setVisibility(View.GONE);
        }

        initRecyclerView();
        initOkgo();
    }

    private void initRecyclerView() {
        recyclerview.setNestedScrollingEnabled(false);
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        mProtocolAdapter = new ProtocolAdapter(R.layout.item_protocol, mList);
        recyclerview.setAdapter(mProtocolAdapter);
        recyclerview.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
        mProtocolAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("content", mList.get(position).getProtocol_content());
                bundle.putString("version", mList.get(position).getVersion());
                bundle.putInt("type", mList.get(position).getType());
                bundle.putString("name", mList.get(position).getProtocol_name());
                RxActivityTool.skipActivity(mContext, ProtocolDetaileActivity.class, bundle);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @OnClick(R.id.tv_qianyue)
    public void onViewClicked() {
//        if (status==3){
            OkGo.<String>post(Config.s + "llc/api/ProtocolController/signProtocols")
                    .tag(this)
                    .params("token", UserBean.getbToken(mContext))
                    .params("type", mList.get(1).getType())
                    .params("type1", mList.get(0).getType())
                    .params("protocol_version", mList.get(1).getVersion())
                    .params("protocol_version1", mList.get(0).getVersion())
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Gson gson = new Gson();
                            BaseBean bean = gson.fromJson(response.body(), BaseBean.class);
                            if (bean.isIsSuccess()) {
                                UserBean.setbAgreement(ProtocolListActivity.this, 0);
                                finish();
                                Intent intent = new Intent(ProtocolListActivity.this, ServerMainActivity.class);
                                startActivity(intent);
                                RxSPTool.putInt(mContext, "LoginType",1);
                                Toast.makeText(mContext, "" + bean.getOutputMessage(), Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(mContext, "" + bean.getOutputMessage(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
//        }else if (status==2){
//            for (ProtocolListBean.DataBean.ProtocolBean protocolBean:mList ) {
//                if (protocolBean.getType()==2){
//                    OkGo.<String>post(Config.s + "llc/api/ProtocolController/signProtocol")
//                            .tag(this)
//                            .params("token", UserBean.getbToken(mContext))
//                            .params("type", 2)
//                            .params("protocol_version",protocolBean.getVersion() )
//                            .execute(new StringCallback() {
//                                @Override
//                                public void onSuccess(Response<String> response) {
//                                    Gson gson = new Gson();
//                                    BaseBean bean = gson.fromJson(response.body(), BaseBean.class);
//                                    if (bean.isIsSuccess()) {
//                                        UserBean.setbAgreement(ProtocolListActivity.this, 0);
//                                        finish();
//                                        Intent intent = new Intent(ProtocolListActivity.this, ServerMainActivity.class);
//                                        startActivity(intent);
//                                        RxSPTool.putInt(mContext, "LoginType",1);
//                                        Toast.makeText(ProtocolListActivity.this, "" + bean.getOutputMessage() , Toast.LENGTH_SHORT).show();
//                                    } else {
//                                        Toast.makeText(ProtocolListActivity.this, "" + bean.getOutputMessage(), Toast.LENGTH_SHORT).show();
//                                    }
//                                }
//                            });
//                }
//            }
//        }else if (status==1){
//            for (ProtocolListBean.DataBean.ProtocolBean protocolBean:mList ) {
//                if (protocolBean.getType()==1){
//                    OkGo.<String>post(Config.s + "llc/api/ProtocolController/signProtocol")
//                            .tag(this)
//                            .params("token", UserBean.getbToken(mContext))
//                            .params("type", 1)
//                            .params("protocol_version",protocolBean.getVersion())
//                            .execute(new StringCallback() {
//                                @Override
//                                public void onSuccess(Response<String> response) {
//                                    Gson gson = new Gson();
//                                    BaseBean bean = gson.fromJson(response.body(), BaseBean.class);
//                                    if (bean.isIsSuccess()) {
//                                        UserBean.setbAgreement(ProtocolListActivity.this, 0);
//                                        finish();
//                                        Intent intent = new Intent(ProtocolListActivity.this, ServerMainActivity.class);
//                                        startActivity(intent);
//                                        RxSPTool.putInt(mContext, "LoginType",1);
//                                        Toast.makeText(ProtocolListActivity.this, "" + bean.getOutputMessage() , Toast.LENGTH_SHORT).show();
//                                    } else {
//                                        Toast.makeText(ProtocolListActivity.this, "" + bean.getOutputMessage(), Toast.LENGTH_SHORT).show();
//                                    }
//                                }
//                            });
//                }
//            }
//        }
    }

    private void initOkgo() {
        OkGo.<String>post(Config.s + "llc/api/ProtocolController/selectSignProtocol")
                .tag(this)
                .params("token", UserBean.getbToken(mContext))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        ProtocolListBean selectClassifyBean = gson.fromJson(response.body(), ProtocolListBean.class);
                        if (selectClassifyBean.isIsSuccess()) {
                            if (selectClassifyBean.getCode()==200){
                                mList.clear();
                                mList.addAll(selectClassifyBean.getData());
                                mProtocolAdapter.setNewData(mList);
                                UserBean.setbAgreement(ProtocolListActivity.this, 0);
                            }else {
                                RxToast.normal(selectClassifyBean.getOutputMessage());
                            }
                        } else {
                            RxToast.error(selectClassifyBean.getOutputMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        RxToast.error("服务器连接错误");
                    }
                });
    }
}
